﻿Public Class AddNewArticle
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Btn_Article_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Article_Submit.Click
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim sql As String = " INSERT INTO Blog (UserID, Tittle, Description, PostDate, TagID ) VALUES (@v1, @v2, @v3, v4, @v5)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", Uid)
        cmd.Parameters.AddWithValue("@v2", txtB_Article_Tittle.Text)
        cmd.Parameters.AddWithValue("@v3", txtB_Article_Description.Text)
        cmd.Parameters.AddWithValue("@v4", Date.Today)
        cmd.Parameters.AddWithValue("@v5", Date.Today.Month)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        txtB_Article_Tittle.Text = String.Empty
        txtB_Article_Description.Text = String.Empty

        Response.Write("<script type='text/javascript'>alert('Successfully posted your article');</script>")
        Response.Redirect("blog.aspx")

    End Sub
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function
End Class